package com.lkm.app_ui.m.Imple.hsj522;

import com.lkm.app_ui.AppConst;
import com.lkm.comlib.task2.DefaultTaskExecutor;
import com.lkm.frame.entity.ResponEntityAbs;
import com.lkm.frame.task2.BackCallAdapter;
import com.lkm.frame.task2.Task;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.frame.task2.TaskCollection;
import com.lkm.frame.task2.TaskCollectionBindTask;
import com.lkm.javatool.NumberHelp;



@Deprecated
class WatchCardBlockTask extends TaskAdapter<Object, Object, String> {

    public static String getTaskId(){
        return WatchCardBlockTask.class.getName();
    }

    private int sectorIndex;
    private int blockIndex;
    private CardCircuitComSe cardCircuitCom;
    private TaskCollection taskCollectionBindTask;


    

    public WatchCardBlockTask(CardCircuitComSe cardCircuitCom, int sectorIndex, int blockIndex) {
        super(new DefaultTaskExecutor(AppConst.getTaskExecutor(), true), getTaskId(), AppConst.getTaskManager2());
        this.cardCircuitCom = cardCircuitCom;
        taskCollectionBindTask = new TaskCollectionBindTask(this);
        this.sectorIndex = sectorIndex;
        this.blockIndex = blockIndex;
    }

    @Override
    public void doExecuting(Object param) {
        super.doExecuting(param);
        tryReadCarNO();
    }
    
    
    private void tryReadCarNO() {
        if(!getIsContinue())return;
        SendGetCardNumCMDTask sendGetCardNumCMDTask = new SendGetCardNumCMDTask(cardCircuitCom);

        sendGetCardNumCMDTask.addBackCall(backCallAdapter);
        sendGetCardNumCMDTask.setParam(null);
        sendGetCardNumCMDTask.action();
        sendGetCardNumCMDTask.execTask();
        taskCollectionBindTask.add(sendGetCardNumCMDTask.getSerialNum());
    }

    private BackCallAdapter<Void, ResponEntityAbs<String>> backCallAdapter = new BackCallAdapter<Void, ResponEntityAbs<String>>() {
        @Override
        protected void onEnd(Task<?, Void, ResponEntityAbs<String>> task, ResponEntityAbs<String> r, boolean isCancel) {
            super.onEnd(task, r, isCancel);
            if (!isCancel && r.isSuccess()) {
                
                tryReadBolck();
            } else {
                if (getIsContinue()) {
                    mTaskExecutor.executeOnDispatchDelayed(new Runnable() {
                        @Override
                        public void run() {
                            tryReadCarNO();
                        }
                    }, 1000);
                }
            }

        }
    };
    

    
    private void tryReadBolck() {
        if(!getIsContinue())return;
        SendReadBlockTask sendGetCardNumCMDTask = new SendReadBlockTask(cardCircuitCom, sectorIndex, blockIndex);
        sendGetCardNumCMDTask.addBackCall(tryReadBolckAdapter);
        sendGetCardNumCMDTask.setParam(null);
        sendGetCardNumCMDTask.action();
        sendGetCardNumCMDTask.execTask();
        taskCollectionBindTask.add(sendGetCardNumCMDTask.getSerialNum());
    }

    private BackCallAdapter<Void, ResponEntityAbs<byte[]>> tryReadBolckAdapter = new BackCallAdapter<Void, ResponEntityAbs<byte[]>>() {
        @Override
        protected void onEnd(Task<?, Void, ResponEntityAbs<byte[]>> task, ResponEntityAbs<byte[]> r, boolean isCancel) {

            if (!isCancel && r.isSuccess()) {
                
                complete(NumberHelp.bytesTohexDesc(r.getData()));
            } else {



                
                complete(null);








            }

        }


    };
    


}
